package com.shengde.jinwawaforleader.model.myJob.classList;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.personalCenter.activity.EditBabyNameActivity;
import com.shengde.jinwawaforleader.model.personalCenter.activity.EditBabySexyActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;

import java.util.Calendar;

public class AddStudentActivity extends BaseActivity {
	private TextView tv_back, fill_add, tv_title;

	private Calendar c = null;
	private DatePickerDialog dialog = null;
	private String classid;
	private TextView studentInfo;  //学生信息
	private RelativeLayout relative_iv_name;  //姓名
	private RelativeLayout relative_iv_baby_sexy; //性别
	private RelativeLayout relative_iv_birthday; //出生日期
	private RelativeLayout relative_iv_nation;  //民族
	private RelativeLayout relative_iv_register_address;  //户口地址
	private RelativeLayout relative_iv_home_address;  //家庭地址
	private RelativeLayout relative_iv_guardian_name; //监护人姓名
	private RelativeLayout relative_iv_guardian_telephone; //监护人电话
	private RelativeLayout relative_iv_guardian_isteacher; //监护人是否是老师
	private RelativeLayout relative_iv_work_address;  //工作单位
	private RelativeLayout relative_iv_remark;  //备注
	private TextView tv_name;
	private TextView tv_sexy;
	private TextView tv_birthday;
	private TextView tv_nation;
	private TextView tv_register_address;
	private TextView tv_home_address;
	private TextView tv_guardian_name;
	private TextView tv_guardian_telephone;
	private TextView tv_guardian_isteacher;
	private TextView tv_work_address;
	private TextView tv_remark;
	private String mEdtStringIsTeacher;
	private String mEdtStringSexy;

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.activity_add_student);
		classid = getIntent().getStringExtra("classid");
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		fill_add = (TextView) findViewById(R.id.fill_add);
		tv_title = (TextView) findViewById(R.id.tv_title);
		studentInfo = (TextView) findViewById(R.id.textview_contact);

		relative_iv_name = (RelativeLayout) findViewById(R.id.relative_iv_name);
		relative_iv_baby_sexy = (RelativeLayout) findViewById(R.id.relative_iv_baby_sexy);
		relative_iv_birthday = (RelativeLayout) findViewById(R.id.relative_iv_birthday);
		relative_iv_nation = (RelativeLayout) findViewById(R.id.relative_iv_nation);
		relative_iv_register_address = (RelativeLayout) findViewById(R.id.relative_iv_register_address);
		relative_iv_home_address = (RelativeLayout) findViewById(R.id.relative_iv_home_address);
		relative_iv_guardian_name = (RelativeLayout) findViewById(R.id.relative_iv_guardian_name);
		relative_iv_guardian_telephone = (RelativeLayout) findViewById(R.id.relative_iv_guardian_telephone);
		relative_iv_guardian_isteacher = (RelativeLayout) findViewById(R.id.relative_iv_guardian_isteacher);
		relative_iv_work_address = (RelativeLayout) findViewById(R.id.relative_iv_work_address);
		relative_iv_remark = (RelativeLayout) findViewById(R.id.relative_iv_remark);

		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_sexy = (TextView) findViewById(R.id.tv_sexy);
		tv_birthday = (TextView) findViewById(R.id.tv_birthday);
		tv_nation = (TextView) findViewById(R.id.tv_nation);
		tv_register_address = (TextView) findViewById(R.id.tv_register_address);
		tv_home_address = (TextView) findViewById(R.id.tv_home_address);
		tv_guardian_name = (TextView) findViewById(R.id.tv_guardian_name);
		tv_guardian_telephone = (TextView) findViewById(R.id.tv_guardian_telephone);
		tv_guardian_isteacher = (TextView) findViewById(R.id.tv_guardian_isteacher);
		tv_work_address = (TextView) findViewById(R.id.tv_work_address);
		tv_remark = (TextView) findViewById(R.id.tv_remark);

	}

	@Override
	public void initData() {
		fill_add.setText(getString(R.string.sure));
		tv_title.setText(getString(R.string.add_student));
		studentInfo.setText(getString(R.string.student_info));
		c = Calendar.getInstance();
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);

		relative_iv_name.setOnClickListener(this);
		relative_iv_baby_sexy.setOnClickListener(this);
		relative_iv_birthday.setOnClickListener(this);
		relative_iv_nation.setOnClickListener(this);
		relative_iv_register_address.setOnClickListener(this);
		relative_iv_home_address.setOnClickListener(this);
		relative_iv_guardian_name.setOnClickListener(this);
		relative_iv_guardian_telephone.setOnClickListener(this);
		relative_iv_guardian_isteacher.setOnClickListener(this);
		relative_iv_work_address.setOnClickListener(this);
		relative_iv_remark.setOnClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				if (TextUtils.isEmpty(tv_name.getText().toString())
						|| TextUtils.isEmpty(tv_guardian_telephone.getText().toString())
						|| TextUtils.isEmpty(tv_birthday.getText().toString())
						|| TextUtils.isEmpty(tv_nation.getText().toString())
						||TextUtils.isEmpty(tv_guardian_name.getText().toString())
						||TextUtils.isEmpty(tv_guardian_telephone.getText().toString())
						||TextUtils.isEmpty(tv_guardian_isteacher.getText().toString())) {

					HomeApplication.showToastShort("带*号选项不能为空!");
					break;
				}
				BaseProtocol.getInstance().addStudentInfo(this,User.getSchoolId(this), classid, tv_name.getText().toString().trim()
						, mEdtStringSexy, tv_birthday.getText().toString().trim(), tv_nation.getText().toString().trim(), tv_register_address.getText().toString().trim()
						, tv_home_address.getText().toString().trim(), tv_guardian_name.getText().toString().trim(), tv_guardian_telephone.getText().toString().trim()
						, mEdtStringIsTeacher, tv_work_address.getText().toString().trim(), tv_remark.getText().toString().trim());
				finish();
				break;
			case R.id.relative_iv_birthday:
				dialog = new DatePickerDialog(
						this,
						new DatePickerDialog.OnDateSetListener() {
							public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
								tv_birthday.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
							}
						},
						c.get(Calendar.YEAR), // 传入年份
						c.get(Calendar.MONTH), // 传入月份
						c.get(Calendar.DAY_OF_MONTH) // 传入天数
				);
				dialog.show();
				break;
			case R.id.relative_iv_name:
				Intent intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "17");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_baby_sexy:
				intent = new Intent(this, EditBabySexyActivity.class);
				intent.putExtra("select_radio", "6");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_nation:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "18");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_register_address:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "19");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_home_address:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "20");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_guardian_name:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "21");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_guardian_telephone:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "22");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_guardian_isteacher:
				intent = new Intent(this, EditBabySexyActivity.class);
				intent.putExtra("select_radio", "7");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_work_address:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "23");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
			case R.id.relative_iv_remark:
				intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "24");
				startActivityForResult(intent, Constant.REQUEST_CODE_ADD_STUDENT);
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Constant.REQUEST_CODE_ADD_STUDENT) {
			if (resultCode == Constant.SETTING_SEXY_SAVE_SUCCESS) {
				mEdtStringSexy = data.getExtras().getString("mSexy");
				tv_sexy.setText(mEdtStringSexy);
				if ("男".equals(mEdtStringSexy)) {
					mEdtStringSexy = "1";
				} else if ("女".equals(mEdtStringSexy)) {
					mEdtStringSexy = "0";
				}
			} else if (resultCode == Constant.SETTING_IS_GUARDIAN_SAVE_SUCCESS) {
				mEdtStringIsTeacher = data.getExtras().getString("isGuardian");
				tv_guardian_isteacher.setText(mEdtStringIsTeacher);
				if (getString(R.string.is_teacher).equals(mEdtStringIsTeacher)) {
					mEdtStringIsTeacher = "1";
				} else if (getString(R.string.is_not_teacher).equals(mEdtStringIsTeacher)) {
					mEdtStringIsTeacher = "0";
				}
			} else if (resultCode == Constant.SETTING_PERSONAL_NAME_TWO) {
				String mEdtStringName = data.getExtras().getString("name_one");
				tv_name.setText(mEdtStringName);
			}else if (resultCode == Constant.SETTING_NATION_SAVE_SUCCESS) {
				String mEdtStringNation = data.getExtras().getString("nation");
				tv_nation.setText(mEdtStringNation);
			} else if (resultCode == Constant.SETTING_REGISTE_ADDRESS_SAVE_SUCCESS) {
				String mEdtStringRegisteAddress = data.getExtras().getString("registe_address");
				tv_register_address.setText(mEdtStringRegisteAddress);
			} else if (resultCode == Constant.SETTING_HOME_ADDRESS_SAVE_SUCCESS) {
				String mEdtStringHomeAddress = data.getExtras().getString("home_address");
				tv_home_address.setText(mEdtStringHomeAddress);
			} else if (resultCode == Constant.SETTING_GURADIAN_NAME_SAVE_SUCCESS) {
				String mEdtStringGuardianName = data.getExtras().getString("guardian_name");
				tv_guardian_name.setText(mEdtStringGuardianName);
			} else if (resultCode == Constant.SETTING_GURADIAN_TELEPHONE_SAVE_SUCCESS) {
				String mEdtStringGuardianTelephone = data.getExtras().getString("guardian_telephone");
				tv_guardian_telephone.setText(mEdtStringGuardianTelephone);
			} else if (resultCode == Constant.SETTING_WORK_ADDRESS_SAVE_SUCCESS) {
				String mEdtStringWorkAddress = data.getExtras().getString("work_address");
				tv_work_address.setText(mEdtStringWorkAddress);
			}else if (resultCode == Constant.SETTING_PERSONAL_REMARE_TWO) {
				String mEdtStringRemark = data.getExtras().getString("remark_two");
				tv_remark.setText(mEdtStringRemark);
			}
		}

	}
}